//
// Created By: Xiaowen.zhu 2021/06/18
//

#pragma once

#include "cybertron/DefinesCore.hpp"
#include <string>

CYBERTRON_BEGIN

    enum FDT_ERROR {
        FDT_ERROR_OK,
        FDT_ERROR_NETWORK,
        FDT_ERROR_FILE,
        FDT_ERROR_DUPLICATE,
        FDT_ERROR_SERVER,
        FDT_ERROR_UNZIP,
        FDT_ERROR_UNKNOWN,
    };

    enum FDT_STAGE {
        FDT_STAGE_QUEUING = 0,
        FDT_STAGE_DONWLOADING = 1,
        FDT_STAGE_WAITING_FOR_RETRY = 2,
        FDT_STAGE_WAITING_FOR_UNZIP = 3,
        FDT_STAGE_UNZIPING = 4,
        FDT_STAGE_FAILED = 5,
        FDT_STAGE_DELETED = 6,
        FDT_STAGE_COMPLETED = 7,
    };

    struct FDT_STATUS {
        FDT_STAGE _stage;
        FDT_ERROR _last_error;
        int _retry_count;
        size_t _total_size;
        size_t _done_size;
    };

    typedef int (*FDT_STATUS_CB)(std::string url, FDT_STATUS status);

    class FileDownloaderTaskManager {
    public:
        CybertronCoreAPI static int
        start(int max_concurrent_task_num = 1, FDT_STATUS_CB callback = NULL);

        CybertronCoreAPI static int stop();

        CybertronCoreAPI static int
        add_task(std::string url, std::string md5sum, std::string dst_dir, bool decompress,
                 FDT_STATUS_CB callback = NULL);

        CybertronCoreAPI static int delete_task(std::string url);

        CybertronCoreAPI static int task_status(std::string url, FDT_STATUS &status);

        CybertronCoreAPI static const char *stage_string(FDT_STAGE stage);

        CybertronCoreAPI static const char *error_string(FDT_ERROR err);
    };

CYBERTRON_END
